// $u.mixin.js

import {
	mapState,
	mapGetters
} from 'vuex'
import store from "@/store"

import router from "@/common/utils/router.js";
import permisson from '@/store/permission.js'
import config from '@/common/config/config.js';
/* 尝试将用户在根目录中的store/index.js的vuex的state变量，全部加载到全局变量中 */
let $CGStoreKey = [];
try {
	$CGStoreKey = store.state ? Object.keys(store.state) : [];
} catch (e) {

}


module.exports = {
	created() {
		/* 	将vuex方法挂在到$u中
		 *	使用方法为：如果要修改vuex的state中的user.name变量为"史诗" => this.$u.vuex('user.name', '史诗')
		 *	如果要修改vuex的state的version变量为1.0.1 => this.$u.vuex('version', '1.0.1') 
		 */
		this.$CGStore = (name, value) => {
			this.$store.commit('$CGStore', {
				name,
				value
			})
		}
		this.$store.commit('SET_RELOAD', false)
	},
	async onLoad(params) {
		permisson()
	},
	filters: {
		noInfo(e) {
			if (e === undefined || e === null || e === '' || !e) {
				return '--'
			} else {
				return e
			}
		},
		rmb(e) {
			return '¥' + e
		},
		hideText(e) {
			if (!e) return ''
			let str = String(e);
			if (e.length > 3) {
				return str.slice(0, str.length - 4) + '****'
			} else {
				return str.slice(0, str.length - 1) + '*'
			}
		},
		hideMobile(mobile) {
			if (!mobile) return ''

			// 定义手机号正则表达式
			let reg = /^(1[3-9][0-9])\d{4}(\d{4}$)/
			// 判断手机号是否能够通过正则校验
			let isMobile = reg.test(mobile);
			if (isMobile) {

				// 将手机号中间4位用*号进行显示
				let hiddenMobile = mobile.replace(reg, "$1****$2")
				return hiddenMobile
			} else {
				return mobile
			}
		}
	},
	computed: {
		/* 将vuex的state中的所有变量，解构到全局混入的mixin中 */
		...mapState($CGStoreKey),
		...mapGetters(store.getters ? Object.keys(store.getters) : [])
	},
	methods: {
		getStaticFilePath(src) {
			if (!src) return console.warn('src is necessary!')
			// const file = src[0] === '/' ? src.substring(1, src.length) : src;
			// const fileSrc = require(`static/${file}`)
			// return fileSrc
			// console.log('file:', src);
			if (src.indexOf('/static/') === 0) {
				// console.log('1:', 1);
				return src
			} else {
				console.log('2:', 2);
				return `/static/${src}`
			}


		},
		/**
		 * 这是一个全局混入的示例方法
		 * @param {string} src - 地址
		 * @param {boolean} forceUseRemote - 是否强制使用远程地址
		 * @returns {string} 返回值,带有https的地址
		 * @example
		 * // 在Vue组件中调用
		 * this.getFilePath('/static/a.png');
		 */
		getFilePath(src, forceUseRemote = false) {
			// 上传的问题地址
			if (!src) return console.warn('src is necessary!')


			if (config.useRemoteFile || forceUseRemote) {
				let httpSrc = ''
				// var reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\/])+$/;
				var strRegex =
					'^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$';
				var reg = new RegExp(strRegex);
				if (reg.test(src)) {
					httpSrc = src
				} else {

					const path = this.getStaticFilePath(src)
					httpSrc = this.filePath + (path[0] === '/' ? path.substring(1, path.length) : path)
				}
				// console.log('httpSrc:',httpSrc);
				return httpSrc
			} else {
				return this.getStaticFilePath(src)
			}

		},
	},
}